<?php
session_start();
require("../lib/utiles.php");
require_once("../conexion/class.conexionDB.inc.php");
require_once("../lib/parametros.php");

if(isset($_GET['id'])) {

    // you may have to modify login information for your database server:
    $conn = new conexionBD ( );
    $conn->SeleccionBBDD($_BASE_SIS);

    $Str_SQL = "SELECT
  persona.PER_FOTO,
  persona.PER_TIPOIMAGEN,
  persona.PER_PESOIMAGEN,
  persona.PER_NOMBREIMAGEN
FROM
  persona
WHERE
  persona.PER_IDPERSONA = $_GET[id]";

    if(!$result = @$conn->EjecutarSQL($Str_SQL)){die("ERROR, no se puede conectar con la base de datos.");}
    $rows = $conn->FetchArray($result);
    $imagen = $rows["PER_FOTO"];
    $nombre = $rows["PER_NOMBREIMAGEN"];
    $tipo = $rows["PER_TIPOIMAGEN"];
    $peso = $rows["PER_PESOIMAGEN"];
    
    if($imagen != "")
    {
        header("Content-type: $tipo");
        header("Content-length: $peso");
        header("Content-Disposition: inline; filename=$nombre");

        echo redimensionarImagen($imagen);
    }
    else
    {
        $nombre = "../images/family.png";
        $fp = fopen($nombre, "rb");
        $peso = filesize($nombre);
        $tfoto = fread($fp, filesize($nombre));
        //$tfoto = addslashes($tfoto);
        fclose($fp);
        header("Content-type: image/png");
        header("Content-length: $peso");
        header("Content-Disposition: inline; filename=$nombre");
        echo redimensionarImagen($tfoto);
    }
   
    function makeThumbnail($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality){
        // Takes the sourcefile (path/to/image.jpg) and makes a thumbnail from it
        // and places it at endfile (path/to/thumb.jpg).

        // Load image and get image size.
        $img = imagecreatefromjpeg($sourcefile);
        $width = imagesx( $img );
        $height = imagesy( $img );

        if ($width > $height) {
            $newwidth = $thumbwidth;
            $divisor = $width / $thumbwidth;
            $newheight = floor( $height / $divisor);
        }
        else {
            $newheight = $thumbheight;
            $divisor = $height / $thumbheight;
            $newwidth = floor( $width / $divisor );
        }

        // Create a new temporary image.
        $tmpimg = imagecreatetruecolor( $newwidth, $newheight );

        // Copy and resize old image into new image.
        imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );

        // Save thumbnail into a file.
        imagejpeg( $tmpimg, $endfile, $quality);

        // release the memory
        imagedestroy($tmpimg);
        imagedestroy($img);
    }
}
?>